package com.tuanzi.loan.business.repository.family.impl;

import com.tuanzi.loan.business.repository.family.custom.LoanLargeAmountGongZhongUserInfoRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.family.largeamount.LoanLargeAmountGzhRequest;
import com.tuanzi.loan.vo.response.family.largeamount.LoanLargeAmountGzhResponse;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanLargeAmountGongzhongUserApply.LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanLargeAmountGongzhongUserInfo.LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO;

/**
 * User: liangZongXi
 * Create time: 10:14 2019/3/29
 * Description:
 */

@Repository
public class LoanLargeAmountGongZhongUserInfoRepositoryImpl extends JOOQBaseRepository implements LoanLargeAmountGongZhongUserInfoRepositoryCustom {
    @Override
    public Page<LoanLargeAmountGzhResponse> list(LoanLargeAmountGzhRequest searchRequest, Pageable pageable) {

        fields(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.fields());
        fields(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.LOAN_COUNT,
                LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.APPLY_COUNT,LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.APPLY_TIME,
                LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.HAS_RED_PACKET,LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.ID,
                LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.GET_RED_PACKET_TIME,LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.HAS_BACK_SECURITIES,
                LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.GET_BACK_SECURITIES_TIME);

        leftJoin(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY,LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.APPLY_ID.eq(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.ID));

        between(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.APPLY_TIME,searchRequest.getStartDate(), searchRequest.getEndDate());

        eq(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.LOAN_AMOUNT, searchRequest.getLoanAmount());
        eq(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.USER_TYPE,searchRequest.getUserType());
        contains(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.WORK_CITY, searchRequest.getWorkCity());
        eq(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.HAS_BACK_SECURITIES, searchRequest.getHasBackSecurities());
        eq(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.HAS_RED_PACKET, searchRequest.getHasRedPacket());

        orderBy(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.APPLY_TIME, Sort.Direction.DESC);

        return search(pageable,new LargeAmountGzhResponse());
    }

    @Override
    public LoanLargeAmountGzhResponse findUserInfoAndUserAppyByUserInfoId(Long userInfoId) {
        fields(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.fields());
        fields(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.LOAN_COUNT,
                LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.APPLY_COUNT,LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.APPLY_TIME,
                LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.HAS_RED_PACKET,LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.ID,
                LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.GET_RED_PACKET_TIME,LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.HAS_BACK_SECURITIES,
                LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.GET_BACK_SECURITIES_TIME);

        leftJoin(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY,LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.APPLY_ID.eq(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.ID));
        eq(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.ID, userInfoId);

        return search(new LargeAmountGzhResponse()).stream().findFirst().orElse(null);
    }

    private final class LargeAmountGzhResponse implements RecordMapper<Record, LoanLargeAmountGzhResponse>{

        @Override
        public LoanLargeAmountGzhResponse map(Record record) {
            LoanLargeAmountGzhResponse response = new LoanLargeAmountGzhResponse();
            response.setId(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.ID));
            response.setApplyId(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.APPLY_ID));
            response.setMobNum(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.MOB_NUM));
            response.setName(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.NAME));
            Byte sexByte = record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.SEX);
            response.setSex(null == sexByte ? null : sexByte.intValue());
            response.setLoanAmount(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.LOAN_AMOUNT));
            response.setUserType(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.USER_TYPE));
            response.setWorkCity(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.WORK_CITY));
            response.setHouseBuy(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.HOUSE_BUY));
            response.setHouseLocation(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.HOUSE_LOCATION));
            response.setCarBuy(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.CAR_BUY));
            response.setCarNumLocation(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.CAR_NUM_LOCATION));
            response.setCarUseTotalTime(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.CAR_USE_TOTAL_TIME));
            response.setCarPrice(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.CAR_PRICE));
            response.setInsurancePrice(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.INSURANCE_PRICE));
            response.setInsuranceUseTotalTime(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.INSURANCE_USE_TOTAL_TIME));
            response.setSalaryPayType(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.SALARY_PAY_TYPE));
            response.setJobCategory(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.JOB_CATEGORY));
            response.setSalaryIncome(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.SALARY_INCOME));
            response.setAccumulationFund(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.ACCUMULATION_FUND));
            response.setSocialSecurity(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.SOCIAL_SECURITY));
            Byte creditByte = record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.HAS_CREDIT_CARD);
            response.setHasCreditCard(creditByte ==null ? null : creditByte.intValue());
            response.setWelidaiAmount(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.WEILIDAI_AMOUNT));
            response.setJiebeiAmount(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO.JIEBEI_AMOUNT));
            response.setLoanCount(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.LOAN_COUNT));
            response.setAppplyCount(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.APPLY_COUNT));
            response.setApplyTime(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.APPLY_TIME));
            Byte redPacketByte = record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.HAS_RED_PACKET);
            response.setHasRedPacket(redPacketByte ==null ? null : redPacketByte.intValue());
            response.setGetRedPacketTime(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.GET_RED_PACKET_TIME));
            Byte hasBackSecurities = record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.HAS_BACK_SECURITIES);
            response.setHasBackSecurities(hasBackSecurities == null ? null : hasBackSecurities.intValue());
            response.setGetBackSecuritiesTime(record.get(LOAN_LARGE_AMOUNT_GONGZHONG_USER_APPLY.GET_BACK_SECURITIES_TIME));

            return response;
        }

    }

    @Override
    protected TableImpl getTable() {
        return LOAN_LARGE_AMOUNT_GONGZHONG_USER_INFO;
    }
}
